package com.example.myletcodelearing.one

import com.example.myletcodelearing.kmp.KmpTest

/**
 * @author tgw
 * @date 2022/9/30
 * @describe
 */
fun main(array: Array<String>) {
    My_1().calculate()

//    val nums=arrayOf(2,1,8,7,2,9)
//
//   val num = My_1().twoSum(nums,9)
//    println("对象：${num?.get(0)}--${num?.get(1)}")
}
private class My_1{
//    var nums = arrayOf(2,7,1,8,2,9)
    var nums = arrayOf(2,1,8,7,2,9)
    val target = 9

     fun calculate(){
        var map = HashMap<Int,Int>()
        var list=ArrayList<NUM>()
        nums.forEach { num ->
            if (num == map[target-num]){
                list.add(NUM(target-num,num))
                for (num in list) {
                    println("对象：${num.toString()}")
                }
              return
            }
            map[num] = target-num

        }


    }

    private class NUM(val a:Int,val b:Int){

        override fun toString(): String {
            return "NUM(a=$a, b=$b)"
        }
    }


    fun twoSum(nums: Array<Int>, target: Int): IntArray? {
        val indexs = IntArray(2)

        // 建立k-v ，一一对应的哈希表
        val hash = HashMap<Int, Int>()
        for (i in nums.indices) {
            if (hash.containsKey(nums[i])) {
                indexs[0] = i
                indexs[1] = hash[nums[i]]!!
                return indexs
            }
            // 将数据存入 key为补数 ，value为下标
            hash[target - nums[i]] = i
        }
        return indexs
    }
}